package com.mvvm.ktnet.base.myappbase

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import com.kingja.loadsir.core.LoadService
import com.mvvm.ktnet.R
import com.mvvm.ktnet.base.activity.IBaseVmActivity
import com.mvvm.ktnet.base.viewmodel.BaseViewModel
import com.mvvm.ktnet.base.viewmodel.LoadingType
import com.mvvm.ktnet.ext.loadServiceInit
import com.mvvm.ktnet.ext.showEmpty
import com.mvvm.ktnet.ext.showError
import com.mvvm.ktnet.ext.showLoading
import com.scwang.smart.refresh.layout.SmartRefreshLayout

/**
 *
 * @ProjectName:    KtNet
 * @Package:        com.mvvm.ktnet.base.myappbase
 * @ClassName:      BaseActivity
 * @Description:     java类作用描述
 * @Author:         Alex
 * @CreateDate:     2020/11/13 10:11
 * @UpdateUser:     更新者：
 * @UpdateDate:     2020/11/13 10:11
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */
abstract class BaseActivity<VM : BaseViewModel> : IBaseVmActivity<VM>() {

    //界面状态管理者
    private var loadsir: LoadService<Any>? = null

    private var loadView: View? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


    }

    var smartRefreshLayout: SmartRefreshLayout? = null


    override fun initRefreshLayout() {
        smartRefreshLayout = findViewById<SmartRefreshLayout>(R.id.smartRefreshLayout)
        smartRefreshLayout?.apply {

        }
    }


    override fun initLoad() {

        loadView = findViewById(R.id.load_root)

        loadsir = loadView?.let {
            loadServiceInit(it) {
                //重试操作
                reLoad()
            }
        }
        loadsir?.showSuccess()
    }

    override fun registorDefErrorChange() {

        mViewModel.loadStatus.loadEmpty.observe(this, Observer {

            onEmpty(it.mathCode, it.throwable)
        })

        mViewModel.loadStatus.loadError.observe(this, Observer {

            onError(it.mathCode, it.throwable)

        })
        mViewModel.loadStatus.loadFinally.observe(this, Observer {

            onFinally(it.mathCode)
        })

    }

    //这里显示dialo等
    override fun registerUiChange() {
        super.registerUiChange()
        dialogObserve(mViewModel)

    }

    override fun addLoadingObserve(vararg viewModels: BaseViewModel) {
        super.addLoadingObserve(*viewModels)
        viewModels.forEach { viewModel ->
            dialogObserve(viewModel)
        }
    }


    fun dialogObserve(viewModel: BaseViewModel) {
        //显示弹窗
        viewModel.loadingChange.showPageLoad.observe(this, Observer {

            when (it.loadingType) {
                LoadingType.LOADING_PAGE -> {
                    when (it.show) {
                        true -> {
                            if (it.empty && !it.error) {
                                showEmpty()
                            } else if (!it.empty && it.error) {
                                showError()
                            } else if (!it.empty && !it.error) {
                                showLoading()
                            }
                        }
                        else -> {
                            showSuccess()
                        }
                    }


                }
                LoadingType.LOADINGDIALOG -> {
                    if (it.show) {
                        if (it.empty || it.error) dismissDialogLoading() else
                            showDialogLoading(it.loadString)
                    } else dismissDialogLoading()
                }

            }
        })

    }

    override fun reLoad() {

    }

    override fun onError(matcCode: Int, throwable: Throwable) {


    }

    override fun secondLoad() {

    }

    override fun onEmpty(matcCode: Int, throwable: Throwable) {


    }

    override fun onFinally(matcCode: Int) {

        smartRefreshLayout?.apply {
            finishLoadMore()
            finishRefresh()
        }
    }

    override fun showLoading(message: String) {
        loadsir?.showLoading()
    }
    override fun showError(message: String) {
        loadsir?.showError()
    }
    override fun showEmpty(message: String) {
        loadsir?.showEmpty()
    }
    override fun showSuccess(message: String) {
        loadsir?.showSuccess()
    }

    private var alertDialog: AlertDialog? = null

    override fun showDialogLoading(message: String) {

        if (alertDialog == null) {
            alertDialog = AlertDialog.Builder(this).create().apply {
                window?.setBackgroundDrawable(ColorDrawable())
                setCancelable(false)
                setContentView(R.layout.loading_alert)
                setCanceledOnTouchOutside(false)
            }

        }
        if (alertDialog?.isShowing == false)
            alertDialog?.show()
    }

    override fun showDialogError(message: String) {
        alertDialog?.dismiss()

    }

    override fun showDialogEmpty(message: String) {
        alertDialog?.dismiss()
    }

    override fun dismissDialogLoading() {
        alertDialog?.dismiss()
    }

}